package com.echoesnet.echoesshortvideo.Fragments;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.Activities.FanAndFocusAct;
import com.echoesnet.echoesshortvideo.Activities.MyEditAct;
import com.echoesnet.echoesshortvideo.Activities.MySettingActivity;
import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Beans.UserBean;
import com.echoesnet.echoesshortvideo.Constant.CommonConstant;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.Image.GlideApp;
import com.echoesnet.echoesshortvideo.Utils.SharePreUtils;
import com.echoesnet.echoesshortvideo.View.BottomSelector;
import com.echoesnet.echoesshortvideo.ViewModels.UserDetailViewModel;
import com.orhanobut.logger.Logger;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author yqh
 * @Date 2018/4/23
 * @Version 1.0
 */
public class UserDetailFragment extends BaseFragment
{
    private final String TAG = UserDetailFragment.class.getSimpleName();

    @BindView(R.id.infoMore)
    TextView infoMore;
    @BindView(R.id.infoUserHeader)
    ImageView infoUserHeader;
    @BindView(R.id.infoUsername)
    TextView infoUsername;
    @BindView(R.id.infoUserIdContent)
    TextView infoUserIdContent;
    @BindView(R.id.infoUserLocation)
    TextView infoUserLocation;
    @BindView(R.id.infoUserGender)
    TextView infoUserGender;
    @BindView(R.id.infoUserXingzuo)
    TextView infoUserXingzuo;
    @BindView(R.id.signature)
    TextView signature;
    @BindView(R.id.infoFansNum)
    TextView infoFansNum;
    @BindView(R.id.infoFocusNum)
    TextView infoFocusNum;
    @BindView(R.id.infoPraiseNum)
    TextView infoPraiseNum;

    @BindView(R.id.infoUserId)
    TextView infoUserId;
    @BindView(R.id.llContent)
    LinearLayout llContent;
    @BindView(R.id.userInfoContent)
    LinearLayout userInfoContent;
    @BindView(R.id.rl_list_video)
    FrameLayout rlListVideo;
    @BindView(R.id.ll_all_fan)
    LinearLayout llAllFan;
    @BindView(R.id.ll_all_focus)
    LinearLayout llAllFocus;
    @BindView(R.id.ll_all_zan)
    LinearLayout llAllZan;
    @BindView(R.id.iv_infoUserGender)
    ImageView ivInfoUserGender;
    @BindView(R.id.ll_all_gender)
    LinearLayout llAllGender;

    private Activity mActivity;
    private UserDetailViewModel uViewModel;
    private Unbinder unbinder;
    private MyVideoListFrg fragment1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.home_info, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        afterViews();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        Logger.t(TAG).d("uID:" + SharePreUtils.getUId());
        if (!TextUtils.isEmpty(SharePreUtils.getUId()))
        {
            uViewModel.requestUserDetail(SharePreUtils.getUId());
            if (fragment1!=null)
            {
                Logger.t(TAG).d("fragment1:走了" );
                fragment1.refreshVideoList("");
            }

        }
    }

    private void afterViews()
    {
        mActivity = getActivity();
        FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        fragment1 = MyVideoListFrg.newVideoListFrg("");
        transaction.add(R.id.rl_list_video, fragment1);
        transaction.commit();

        uViewModel = ViewModelProviders.of(this).get(UserDetailViewModel.class);
        uViewModel.getUserDetail().observe(this, new Observer<UserBean>()
        {
            @Override
            public void onChanged(@Nullable UserBean user)
            {
                Logger.t(TAG).d("user:" + user.toString());
                if (user != null)
                {
                    infoUsername.setText(user.getNickname());
                    infoUserIdContent.setText(user.getUserId());
                    if (!TextUtils.isEmpty(user.getCity()))
                    {
                        infoUserLocation.setVisibility(View.VISIBLE);
                        infoUserLocation.setText(user.getCity());
                    }
                    else
                    {
                        infoUserLocation.setVisibility(View.GONE);
                    }
                    if (TextUtils.isEmpty(user.getAge())&&TextUtils.isEmpty(user.getSex()))
                    {
                        llAllGender.setVisibility(View.GONE);
                    }else
                    {
                        llAllGender.setVisibility(View.VISIBLE);
                        infoUserGender.setText(user.getAge());
                        ivInfoUserGender.setBackgroundResource("男".equals(user.getSex()) ? R.drawable.but_man : R.drawable.but_woman);
                        llAllGender.getChildAt(0).setVisibility(TextUtils.isEmpty(user.getSex())?View.GONE:View.VISIBLE);
                        llAllGender.getChildAt(1).setVisibility(TextUtils.isEmpty(user.getAge())?View.GONE:View.VISIBLE);
                    }
                    infoUserXingzuo.setVisibility(TextUtils.isEmpty(user.getConstellation())?View.GONE:View.VISIBLE);
                    infoUserXingzuo.setText(user.getConstellation());
                }
                signature.setText(TextUtils.isEmpty(user.getSignature()) ? "Ta还没有想到写点啥~" : user.getSignature());
                infoFansNum.setText(user.getFans());
                infoFocusNum.setText(user.getFollow());
                infoPraiseNum.setText(user.getLikes());

                GlideApp.with(AppController.getInstance())
                        .load(user.getHeadPic())
                        .circleCrop()
                        .error(R.drawable.default_80px)
                        .into(infoUserHeader);

            }
        });
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser)
    {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    protected String getPageName()
    {
        return TAG;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == CommonConstant.RESULT_CODE_OK)
        {
            switch (requestCode)
            {
                case CommonConstant.REQUEST_CODE_LOGIN:

                    break;
            }
        } else
        {

        }
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.infoMore, R.id.infoFansNum, R.id.infoFocusNum, R.id.infoPraiseNum, R.id.ll_all_fan, R.id.ll_all_focus, R.id.ll_all_zan})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.ll_all_fan:
            case R.id.infoFansNum://粉丝数
                if (CommonUtils.checkUserStateFromFragment(UserDetailFragment.this, null))
                {
                    Intent fanIntent = new Intent(getActivity(), FanAndFocusAct.class);
                    fanIntent.putExtra("type", "fan");
                    fanIntent.putExtra("tId", SharePreUtils.getUId());
                    startActivity(fanIntent);
                }
                break;
            case R.id.ll_all_focus:
            case R.id.infoFocusNum://关注数
                if (CommonUtils.checkUserStateFromFragment(UserDetailFragment.this, null))
                {
                    Intent editIntent = new Intent(getActivity(), FanAndFocusAct.class);
                    editIntent.putExtra("type", "follow");
                    editIntent.putExtra("tId", SharePreUtils.getUId());
                    startActivity(editIntent);
                }
                break;
            case R.id.ll_all_zan:
            case R.id.infoPraiseNum://点赞数
                break;
            case R.id.infoMore:
                initPopWindow();
                break;
            default:
                break;
        }
    }

    private void initPopWindow()
    {
        new BottomSelector(mActivity, Arrays.asList(new String[]{"编辑资料", "设置"}))
                .setFirstClickListener((view, pop) -> {
                    if (CommonUtils.checkUserStateFromFragment(UserDetailFragment.this, null))
                    {
                        pop.dismiss();
                        Intent editIntent = new Intent(getActivity(), MyEditAct.class);
                        startActivity(editIntent);
                    }
                }).setSecondClickListener((view, pop) -> {
            if (CommonUtils.checkUserStateFromFragment(UserDetailFragment.this, null))
            {
                pop.dismiss();
                Intent setIntent = new Intent(getActivity(), MySettingActivity.class);
                startActivity(setIntent);
            }
        }).show(infoMore);
    }
}
